package com.xys.auth.utils;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;

public class DateTimeUtils {
    private static final String DEFAULT_DATETIME_PATTERN = "yyyy/MM/dd HH:mm:ss";

    /**
     * 将时间戳转换为日期字符串(默认格式)
     * @param num 时间戳
     */
    public static String castLongToDateStr(Long num) {
        if (num == null || num == 0L) {
            return "";
        }
        return castToDateStr(new Date(num));
    }

    /**
     *
     * @param num 时间戳
     * @param pattern 转换格式
     */
    public static String castLongToDateStr(Long num, String pattern) {
        if (num == null || num == 0L) {
            return "";
        }
        return castToDateStr(new Date(num), pattern);
    }


    /**
     * 转换Date为LocalDateTime
     * @param date Date
     * @return LocalDateTime
     */
    public static LocalDateTime castDateToLocalDateTime(Date date) {

        return Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDateTime();
    }

    /**
     * 转换时间文字为LocalDate
     * 自定义参数
     * @param dateStr 时间字符串
     * @param pattern 转换格式
     * @return LocalDateTime
     */
    public static LocalDateTime castDateStrToLocalDateTime(String dateStr, String pattern) {

        return LocalDateTime.parse(dateStr, DateTimeFormatter.ofPattern(pattern));
    }

    public static LocalDate castDateStrToLocalDate(String dateStr, String pattern) {

        return LocalDate.parse(dateStr, DateTimeFormatter.ofPattern(pattern));
    }

    /**
     * 转换时间文字为LocalDate
     * 默认参数
     * @param dateStr 时间字符串
     * @return LocalDateTime
     */
    public static LocalDateTime castDateStrToLocalDateTime(String dateStr) {

        return castDateStrToLocalDateTime(dateStr, DEFAULT_DATETIME_PATTERN);
    }

    /**
     * 格式化LocalDateTime为日期字符串
     * 自定义格式
     * @param localDateTime LocalDateTime
     * @param pattern 格式
     * @return 格式化日期字符串
     */
    public static String castToDateStr(LocalDateTime localDateTime, String pattern) {

        return localDateTime.format(DateTimeFormatter.ofPattern(pattern));
    }

    /**
     * 格式化LocalDateTime为日期字符串
     * 默认转换格式
     * @param localDateTime LocalDateTime
     * @return 格式化日期字符串
     */
    public static String castToDateStr(LocalDateTime localDateTime) {

        return castToDateStr(localDateTime, DEFAULT_DATETIME_PATTERN);
    }

    /**
     * 格式化Date为日期字符串
     * 自定义格式
     * @param date Date
     * @param pattern 格式
     * @return 格式化日期字符串
     */
    public static String castToDateStr(Date date, String pattern) {

        LocalDateTime localDateTime = castDateToLocalDateTime(date);
        return castToDateStr(localDateTime, pattern);
    }

    /**
     * 格式化Date为日期字符串
     * 默认转换格式
     * @param date Date
     * @return 格式化日期字符串
     */
    public static String castToDateStr(Date date) {

        if (date == null) {
            return "";
        }
        return castToDateStr(date, DEFAULT_DATETIME_PATTERN);
    }

    /**
     * 转换LocalDateTime 为 Date
     * @param localDateTime LocalDateTime
     * @return Date
     */
    public static Date castLocalDateTimeToDate(LocalDateTime localDateTime) {

        return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
    }

    /**
     * 根据月获取季度
     * @param month
     * @return
     */
    public static Integer getQuarterByMonth(Integer month) {

        if (month != null && month > 0) {

            return new Double(Math.ceil(month / 3.0)).intValue();
        } else {
            return 0;
        }
    }

    /**
     * 返回指定日期的季的第一天
     */
    public static Date getFirstDayOfQuarter(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return getFirstDayOfQuarter(calendar.get(Calendar.YEAR),
                getQuarterOfYear(date));
    }

    /**
     * 返回指定日期的季度
     * @param date
     * @return
     */
    public static int getQuarterOfYear(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.MONTH) / 3 + 1;
    }

    /**
     * 返回指定年季的季的第一天
     */
    public static Date getFirstDayOfQuarter(Integer year, Integer quarter) {
        Calendar calendar = Calendar.getInstance();
        Integer month = new Integer(0);
        if (quarter == 1) {
            month = 1 - 1;
        } else if (quarter == 2) {
            month = 4 - 1;
        } else if (quarter == 3) {
            month = 7 - 1;
        } else if (quarter == 4) {
            month = 10 - 1;
        } else {
            month = calendar.get(Calendar.MONTH);
        }
        return getFirstDayOfMonth(year, month);
    }

    /**
     * 返回指定日期的月的第一天
     */
    public static Date getFirstDayOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(calendar.get(Calendar.YEAR),
                calendar.get(Calendar.MONTH), 1);
        return calendar.getTime();
    }

    /**
     * 返回指定年月的月的第一天
     */
    public static Date getFirstDayOfMonth(Integer year, Integer month) {
        Calendar calendar = Calendar.getInstance();
        if (year == null) {
            year = calendar.get(Calendar.YEAR);
        }
        if (month == null) {
            month = calendar.get(Calendar.MONTH);
        }
        calendar.set(year, month, 1);
        return calendar.getTime();
    }

    /**
     * 将LocalDate 转换成 Date
     * @param localDate
     * @return
     */
    public static Date localDateToDate(LocalDate localDate){
        ZoneId zoneId = ZoneId.systemDefault();
        ZonedDateTime zdt = localDate.atStartOfDay(zoneId);
        return Date.from(zdt.toInstant());
    }

    /**
     * 将date转换成String
     */
    public static String dateFormat(Date date,String pattern){
        return localDateTimeFormat(dateToLocalDateTime(date),pattern);
    }

    /**
     * 将localDateTime 按照一定的格式转换成String
     */
    public static String localDateTimeFormat(LocalDateTime localDateTime, String pattern){
        return localDateTime.format(DateTimeFormatter.ofPattern(pattern));
    }
    /**
     * 将 Date 转换成LocalDateTime
     * atZone()方法返回在指定时区从此Instant生成的ZonedDateTime。
     */
    public static LocalDateTime dateToLocalDateTime(Date date){
        Instant instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        return instant.atZone(zoneId).toLocalDateTime();
    }
}
